package fifthDay;

import java.util.ArrayList;
import java.util.List;

public class FindAnagrams {
    public static void main(String[] args) {
        String s = "abab", p = "ab";
        List<Integer> ans = findAnagrams(s,p);
        for (Integer an : ans) {
            System.out.println(an);
        }
    }
    public static List<Integer> findAnagrams(String s, String p) {
        List<Integer> ans = new ArrayList<>();
        if (p.length() > s.length()) {
            return ans;
        }
        int[] cont = new int[26];
        for (int i = 0; i < p.length(); i++) {
            --cont[p.charAt(i) - 'a'];
        }
        int left = 0;
        for (int i = 0; i < s.length(); i++) {
            cont[s.charAt(i) - 'a']++;
            while (cont[s.charAt(i) - 'a'] > 0){
                cont[s.charAt(left) - 'a']--;
                left++;
            }
            if (i - left + 1 == p.length()){
                ans.add(left);
            }
        }
        return ans;
    }
}
